const express = require("express");
const cors = require("cors");
const app = express();
const port = 3000;

app.use(cors());
app.use('/static', express.static('public'));
app.get("/stream", (req, res) => {
  res.writeHead(200, {
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache",
    Connection: "keep-alive",
  });

  let i = 0;
  const interval = setInterval(() => {
    const data = `data: ${i++}\n\n`;
    res.write(data);
    if (i > 10) {
      clearInterval(interval);
      res.end();
    }
  }, 1000);

  req.on("close", () => {
    clearInterval(interval);
  });
});

app.listen(port, () => {
  console.log(`Streaming server listening on port ${port}`);
});